<?php

class MatchController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
		$this->beforeFilter('auth', array('except' => array('getAPI', 'getAdminUserAPI', 'getTeamsAPI', 'getSubsAPI',
					'updateSubsAPI', 'getGuestsAPI', 'updateGuestsAPI', 'storeAPI', 'updateAPI', 'destroyAPI')));
	}
	
	/**
	 * Display a listing of matches.
	 *
	 * @return Response
	 */
	public function index()
	{
		// get all the matches
		$matches = Match::all();
		// get all the match types
		$matchTypes = MatchType::all();
		
		foreach($matches as $key => $value){
			//count guests
			$guestsAmount = $value->guests->count();
			$value->guestsAmount = $guestsAmount;
			
			//count guests confirmed
			$guestsConfirmedAmount = $value->guests()->wherePivot('confirmed', 1)->count();
			$value->guestsConfirmedAmount = $guestsConfirmedAmount;
				
			//count guests not going
			$guestsNotGoingAmount = $value->guests()->wherePivot('confirmed', 0)->count();
			$value->guestsNotGoingAmount = $guestsNotGoingAmount;
		}
		
		// load the view and pass the matches
		return View::make('matches.index')
			->with('matches', $matches)
			->with('matchTypes', $matchTypes);
	}

	/**
	 * Show the form for creating a new match.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created match in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function store()
	{
		//
	}


	/**
	 * Display the specified match.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		// get the field
		$match = Match::find($id);

		// show the edit form and pass the field
		return View::make('matches.show')
			->with('match', $match);
	}


	/**
	 * Show the form for editing the specified match.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified match in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function update()
	{
		//
	}


	/**
	 * Remove the specified match from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroy($id)
	{
		//
	}
	
	/*
	 * API Methods
	*/
	/**
	 * Display a listing of matches in JSON format.
	 *
	 * @return JSON Response
	 */
	public function getAPI($id = null)
	{
		if (is_null($id))
			return Response::json(Match::all());
		else
		{
			$match = Match::find($id);
			if(is_null($match)){
				return Response::json('Match not found', 404);
			} else {
				return Response::json($match);
			}
		}
	}
	
	/**
	 * Display the admin user of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getAdminUserAPI($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			return Response::json($match->admin);
		}
	}
	
	/**
	 * Display the teams and its users of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getTeamsAPI($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			foreach($match->teams as $value){
				$value->users;
			}
			return Response::json($match->teams);
		}
	}
	
	/**
	 * Display the substitute users of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getSubsAPI($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			return Response::json($match->substitutes);
		}
	}
	
	/**
	 * Display the guest users of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getGuestsAPI($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			return Response::json($match->guests);
		}
	}
	
	/**
	 * Store a newly created match in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function storeAPI()
	{
		$newMatch = Input::json();
	
		$match = new Match();
		$match->date = $newMatch->get('date');
		$match->comments = $newMatch->get('comments');
		$match->fieldId = $newMatch->get('fieldId');
		$match->matchTypeId = $newMatch->get('matchTypeId');
		$match->admin_userId = $newMatch->get('admin_userId');
		$match->cancelled = $newMatch->get('cancelled');
		$match->save();
	
		return Response::json($match);
	}
	
	/**
	 * Update the specified match in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function updateAPI()
	{
		$updateMatch = Input::json();
	
		$match = Match::find($updateMatch->get('id'));
		if(is_null($match)){
			return Response::json('Match not found', 404);
		}
		$match->date = $updateMatch->get('date');
		$match->comments = $updateMatch->get('comments');
		$match->fieldId = $updateMatch->get('fieldId');
		$match->matchTypeId = $updateMatch->get('matchTypeId');
		$match->admin_userId = $updateMatch->get('admin_userId');
		$match->cancelled = $updateMatch->get('cancelled');
		$match->save();
	
		return Response::json($match);
	}
	
	/**
	 * Update substitute specified users into specified team by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateSubsAPI()
	{
		$data = Input::json();
	
		// store relation to substitute Users
		$matchId = $data->get('matchId');
		$match = Match::find($matchId);
		$userIds = $data->get('userIds');
		if(is_array($userIds))
		{
			$match->substitutes()->sync($userIds);
		}
	
		return Response::json($match->substitutes);
	}
	
	/**
	 * Update guest specified users into specified team by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateGuestsAPI()
	{
		$data = Input::json();
	
		// store relation to substitute Users
		$matchId = $data->get('matchId');
		$match = Match::find($matchId);
		$userIds = $data->get('userIds');
		if(is_array($userIds))
		{
			$match->guests()->sync($userIds);
		}
	
		return Response::json($match->guests);
	}
	
	/**
	 * Remove the specified match from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroyAPI($id)
	{
		$match = Match::find($id);
	
		if(is_null($match))
		{
			return Response::json('Match not found', 404);
		}
		$deletedMatch = $match;
		$match->delete();
		return Response::json($deletedMatch);
	}

}
